package logic

import (
	"context"

	"github.com/comfygpt-backend-go/common/consts"
	"github.com/comfygpt-backend-go/common/utils"

	"github.com/comfygpt-backend-go/service/component-api/internal/svc"
	"github.com/comfygpt-backend-go/service/component-api/internal/types"
	"github.com/comfygpt-backend-go/service/component-rpc/component"

	"github.com/zeromicro/go-zero/core/logx"
)

type GetComponentByIdLogic struct {
	logx.Logger
	ctx    context.Context
	svcCtx *svc.ServiceContext
}

func NewGetComponentByIdLogic(ctx context.Context, svcCtx *svc.ServiceContext) *GetComponentByIdLogic {
	return &GetComponentByIdLogic{
		Logger: logx.WithContext(ctx),
		ctx:    ctx,
		svcCtx: svcCtx,
	}
}

func (l *GetComponentByIdLogic) GetComponentById(req *types.ComponentIdRequest) (resp *types.CommonResp, err error) {
	// 初始化响应
	resp = &types.CommonResp{
		Code:    int64(consts.SUCCESS),
		Message: consts.WrongMessageEn[consts.SUCCESS],
	}
	// 获取组件详情
	res, err := l.svcCtx.ComponentRpc.GetComponentById(l.ctx, &component.ComponentIdRequest{Id: req.Id})
	if err != nil {
		resp.Code = int64(consts.SERVER_WRONG)
		resp.Message = consts.WrongMessageEn[consts.SERVER_WRONG]
		return
	}

	resp.Data = &types.ComponentItem{
		Id:          res.Id,
		Name:        res.Name,
		Description: res.Description,
		Category:    res.Category,
		Icon:        res.Icon,
		Tags:        utils.ArrayToString(res.Tags),
	}

	return
}
